﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class UserCompDao
    {


        public UserCompDao()
        {

            Mapper.CreateMap<E_UserComp, UserCompModel>();
        }
        public List<E_UserComp> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_UserComp.Count();

                return edm.E_UserComp.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public UserCompModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_UserComp.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_UserComp, UserCompModel>(query);
            }

        }


        public int Save(UserCompModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_UserComp.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_UserComp();

                    edm.AddToE_UserComp(query);

                }


                query.CompEname = obj.CompEname;

                query.CompName = obj.CompName;

                query.CompLogo = obj.CompLogo;

                query.PostCode = obj.PostCode;

                query.Contact = obj.Contact;

              

                query.PhoneStart = obj.PhoneStart;

                query.PhoneNum = obj.PhoneNum;

                query.FaxStart = obj.FaxStart;

                query.FaxNum = obj.FaxNum;

                query.UserId = obj.UserId;

                query.IsDelete = obj.IsDelete;

                //query.CreateUserId = obj.CreateUserId;

                query.SiteID = obj.SiteID;

                query.CompLevel = obj.CompLevel;

               
                 
                edm.SaveChanges();

                return query.Id;


            }

        }


        public List<UserCompModel> GetUserComp(int tpnum, int tp)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities()) {
                return Mapper.Map<List<E_UserComp>, List<UserCompModel>>(edm.E_UserComp.OrderByDescending(s => s.Id).Skip(0).Take(tpnum).ToList());
            }
        }

        /// <summary>
        /// 根据用户等级排序
        /// </summary>
        /// <param name="cmpname"></param>
        /// <param name="startIndex"></param>
        /// <param name="maxRow"></param>
        /// <param name="recordcount"></param>
        /// <returns></returns>
        public List<UserCompModel> GetUserComp(string cmpname, int startIndex, int maxRow, out int recordcount)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                var query = from s in edm.E_UserComp
                            select s;
                if (!string.IsNullOrEmpty(cmpname)) {
                    query = query.Where(s=>s.CompName.IndexOf(cmpname)>=0);
                }
                recordcount = query.Count();

                return Mapper.Map<List<E_UserComp>, List<UserCompModel>>(query.OrderByDescending(s => s.CompLevel).Skip(startIndex).Take(maxRow).ToList());
            }
        }

        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_UserComp.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

